define(["jquery","servers"], function ($,servers) {
    return Backbone.Model.extend({
        defaults: function () {
            return {
            };
        },
        getPageList:function(param){
            var self = this;
            var paramMap=servers.getPageParamMap();
            paramMap= $.ExtCommon.initMergeJson(paramMap,{"advertiserId":"customer"});
            var newParam= $.ExtCommon.getMappingJson(param,paramMap);
            servers.targetPack.pageList(newParam,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data.list,function (info) {
                        var newInfo=$.ExtCommon.getMappingJson(info,{
                            id:"id",name:"name",strategyCount:"strategyNum",customerName:"advertiserName",
                            targetTime:"hourT",targetArea:"geoT",targetAudience:"personalT",targetInterest:"businessT",targetDevice:"mobileT",
                            targetAppAction:"appT",targetKeyword:"keywordT",targetCrowd:"customAudienceT"
                        },true);
                        var targetTag=[];
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetTime)) {targetTag.push("时间")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetArea)) {targetTag.push("地域")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetAudience)) {targetTag.push("人口属性")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetInterest)) {targetTag.push("商业兴趣")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetDevice)) {targetTag.push("移动设备")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetAppAction)) {targetTag.push("app行为")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetKeyword)) {targetTag.push("关键字")}
                        if(!$.ExtCommon.isCheckEmpty(newInfo.targetCrowd)) {targetTag.push("自定义人群")}
                        newInfo.targetTag=targetTag;
                        return newInfo;
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data.list=newJson;
                self.set({dataPageList: res});
                self.trigger('bind:PageList');
            });
        },
        getAllList:function (param) {
            var self = this;
            var newParam= $.ExtCommon.getMappingJson(param,{campaignId:"campaignId"});
            servers.targetPack.list(newParam,function(res){
                var group={};
                var list=[];
                if(res.success){
                    for(var i=0;i<res.data.length;i++){
                        var newInfo=$.ExtCommon.getMappingJson(res.data[i],{id:"id",name:"name",objMatterType:"type"},true);
                        if(!$.ExtCommon.isCheckEmpty(newInfo.objMatterType)){
                            var type=newInfo.objMatterType.toString();
                            if(!group.hasOwnProperty(type)){group[type]=[]}
                            group[type].push(newInfo);
                        }
                        list.push(newInfo)
                    }
                }
                self.set({dataAllList: servers.getSendSuccess({list:list,group:group})});
                self.trigger('bind:AllList');
            });
        },
        getStrategyList:function(param){
            var self = this;
            servers.targetPack.strategyList(param.id,function(res){
                var newJson=[];
                if(res.success){
                    newJson=$.map(res.data,function (info) {
                        return $.ExtCommon.getMappingJson(info,{
                            strategyName:"strategyName",campaignName:"campaignName",statusName:"strategyStatusDesc"
                        },true);
                    });
                }else{
                    res.data={};
                }
                res.dataOriginal=res.data;
                res.data=newJson;
                self.set({dataStrategyList:{req:param,res:res}});
                self.trigger('bind:StrategyList');
            });
        },
        getInfo: function (id) {
            var self = this;
            if (id) {
                servers.targetPack.info(id, function (res) {
                    var newJson = $.ExtCommon.getMappingJson(res.success?res.data:{}, {
                       id:"id",name:"name",description:"description",targetData:"targetParam",customer:"advertiserId",objMatterType:"adType"
                    },true);
                    var typeMap=servers.base.getMapObjMatterType(newJson.objMatterType.toString()).idMap;
                    newJson.isOrdinary=typeMap==="1";
                    newJson.isWeChat=typeMap==="2";
                    newJson.targetData=$.ExtCommon.getMappingJson(newJson.targetData, {
                        time:"hours",area:"area",interest:"interests",gender:"gender",education:"education",marriage:"marriage",age:"age",
                        connection:"networkType",os:"os",telCom:"networkOperator",keyword:"keyword",appAction:"app",crowd:"customAudience"
                    });
                    if(!$.ExtCommon.isCheckEmpty(newJson.targetData.appAction)){
                        newJson.targetData.appAction=$.ExtCommon.getMappingJson(newJson.targetData.appAction,{category:"objects",action:"acts",fromDay:"timeWindow"})
                    }
                    if(!$.ExtCommon.isCheckEmpty(newJson.targetData.crowd)){
                        newJson.targetData.crowd=newJson.targetData.crowd.list;
                    }
                    res.dataOriginal=res.data;
                    res.data=newJson;
                    self.set({dataInfo: {isAdd:false,res:res}});
                    self.trigger('bind:Info');
                });
            } else {
                var dataDef={isOrdinary:true,targetData:{}};
                dataDef.objMatterType=servers.base.getMapObjMatterType("1",true);
                self.set({dataInfo:{isAdd:true,res:servers.getSendSuccess(dataDef)}});
                self.trigger('bind:Info');
            }
        },
        addInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            servers.targetPack.add(data, function (res) {
                self.set({dataSave: {isAdd: true, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        updateInfo: function (json) {
            var self = this;
            var data=this._getMappingToServer(json);
            data.id=json.id;
            servers.targetPack.update(data, function (res) {
                self.set({dataSave: {isAdd: false, req: json, res: res}});
                self.trigger('save:Info');
            });
        },
        _getMappingToServer:function(json){
            var newJson = $.ExtCommon.getMappingJson(json, {name:"name",description:"description",advertiserId:"customer",adType:"objMatterType"});
            newJson.targetParam=$.ExtCommon.getMappingJson(json.targetData, {
                hours:"time",area:"area",interests:"interest",gender:"gender",education:"education",marriage:"marriage",age:"age",
                connection:"connection",os:"os",telcom:"telCom",keyword:"keyword",customAudience:"crowd"
            });
            if(!$.ExtCommon.isCheckEmpty(json.targetData.appAction)){
                newJson.targetParam.behavior=$.ExtCommon.getMappingJson(json.targetData.appAction, {objectIds:"category",actIds:"action",timeWindow:"fromDay"});
            }
            return newJson
        },
        deleteInfo: function (id) {
            var self = this;
            servers.targetPack.delete(id,function (res) {
                self.set({dataDeleteInfo: res});
                self.trigger('delete:Info');
            });
        }
    });
});